package com.ruoyi.web.controller.market.extend;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ruoyi.db.domain.ChannelUnionInfo;
import com.ruoyi.db.domain.ProductInfo;
import com.ruoyi.db.model.param.ChannelInfoParam;
import com.ruoyi.extend.service.IChannelInfoService;
import com.ruoyi.extend.service.IChannelUnionInfoService;
import com.ruoyi.market.http.service.MarketApiService;
import com.ruoyi.product.service.IProductInfoService;
import com.ruoyi.web.wapper.ChannelUnionInfoWapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 联登渠道信息Controller
 * 
 * @author ruoyi
 * @date 2022-10-19
 */
@Controller
@RequestMapping("/extend/unioninfo")
public class ChannelUnionInfoController extends BaseController
{
    private String prefix = "extend/unioninfo";

    @Autowired
    private IChannelUnionInfoService channelUnionInfoService;
    @Autowired
    private IChannelInfoService channelInfoService;
    @Autowired
    private MarketApiService marketApiService;
    @Autowired
    private IProductInfoService productInfoService;

    @RequiresPermissions("extend:unioninfo:view")
    @GetMapping()
    public String unioninfo()
    {
        // 查询联登类型的渠道信息
        return prefix + "/unioninfo";
    }

    /**
     * 查询联登渠道信息列表
     */
    @RequiresPermissions("extend:unioninfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ChannelUnionInfo channelUnionInfo)
    {

        ProductInfo productInfo = new ProductInfo();
        productInfo.setUnionLoginFlag(1);
        List<Map<String, Object>> productMapList = productInfoService.listMap(productInfo);

        startPage();
        List<Map<String, Object>> mapList = channelUnionInfoService.selectChannelUnionInfoListMap(channelUnionInfo);
        mapList = new ChannelUnionInfoWapper(mapList)
                .setProductList(productMapList)
                .wrap();

        return getDataTable(mapList);
    }

    /**
     * 导出联登渠道信息列表
     */
    @RequiresPermissions("extend:unioninfo:export")
    @Log(title = "联登渠道信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ChannelUnionInfo channelUnionInfo)
    {
        List<ChannelUnionInfo> list = channelUnionInfoService.selectChannelUnionInfoList(channelUnionInfo);
        ExcelUtil<ChannelUnionInfo> util = new ExcelUtil<ChannelUnionInfo>(ChannelUnionInfo.class);
        return util.exportExcel(list, "联登渠道信息数据");
    }

    /**
     * 新增联登渠道信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        ChannelInfoParam channelInfoParam = new ChannelInfoParam();
        channelInfoParam.setUnionFlag(1);

        List<Map<String, String>> channelInfoList =  channelInfoService.selectSimpleMapInfo(channelInfoParam);
        mmap.put("channelInfoList", channelInfoList);
        return prefix + "/add";
    }

    /**
     * 新增保存联登渠道信息
     */
    @RequiresPermissions("extend:unioninfo:add")
    @Log(title = "联登渠道信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ChannelUnionInfo channelUnionInfo)
    {
        return toAjax(marketApiService.createChannelUnion(channelUnionInfo.getChannelCode()));
    }

    /**
     * 修改联登渠道信息
     */
    @RequiresPermissions("extend:unioninfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {

        ChannelUnionInfo channelUnionInfo = channelUnionInfoService.selectChannelUnionInfoById(id);
        mmap.put("channelUnionInfo", channelUnionInfo);

        ProductInfo productInfo = new ProductInfo();
        productInfo.setUnionLoginFlag(1);

        JSONArray jsonArray = JSON.parseArray(channelUnionInfo.getUnionProdIds());
        List<Map<String, Object>> productMapList = productInfoService.listMap(productInfo);
        for (Map<String, Object> map: productMapList){
            Long value = (Long) map.get("value");
            if (ObjectUtils.isNotEmpty(jsonArray) && jsonArray.contains(String.valueOf(value))){
                map.put("flag", true);
            }else{
                map.put("flag", false);
            }
        }
        mmap.put("productList", productMapList);

        return prefix + "/edit";
    }

    /**
     * 修改保存联登渠道信息
     */
    @RequiresPermissions("extend:unioninfo:edit")
    @Log(title = "联登渠道信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChannelUnionInfo channelUnionInfo)
    {
        return toAjax(channelUnionInfoService.updateChannelUnionInfo(channelUnionInfo));
    }

    /**
     * 删除联登渠道信息
     */
    @RequiresPermissions("extend:unioninfo:remove")
    @Log(title = "联登渠道信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(channelUnionInfoService.deleteChannelUnionInfoByIds(ids));
    }
}
